package org.grayrabbit.cms.properties;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Collections;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.module.propertyset.PropertySet;


/**
 * @author sys53
 * @version 1.0
 * @created 06-一月-2012 16:51:48
 */
public class ApplicationPropertiesImpl implements ApplicationProperties {
	private static final Logger log = LoggerFactory.getLogger(ApplicationPropertiesImpl.class);
	PropertiesManager pm;
	private Properties defaultProperties;
	private static final String APP_PROPERTIES_DEFAULTS = "cms-appliction.properties";
	private boolean propsManagerInitialized = false;
	
	private PropertiesManager getPropertiesManager() {
		if(this.pm == null){
			if(!this.propsManagerInitialized){
				this.pm = PropertiesManager.getInstance();
				propsManagerInitialized = true;
			}
		}
		return this.pm;
	}
	
	/**
	 * 
	 * @param key
	 */
	public boolean exists(String key){
		PropertiesManager propertiesManager = getPropertiesManager();
		if(propertiesManager != null){
			return propertiesManager.getPropertySet().exists(key);
		}
		return false;
	}

	/**
	 * 
	 * @param key
	 */
	public String getDefaultString(String key){
		return getDefaultProperties().getProperty(key);
	}

	public String getEncoding(){
		String encoding = getString("i18n.encoding");
		if(StringUtils.isBlank(encoding)){
			encoding = "UTF-8";
			setString("i18n.encoding",encoding);
		}
		return encoding;
	}

	/**
	 * 
	 * @param name
	 */
	public String getString(String name){
		PropertiesManager propertiesManager = getPropertiesManager();
		if(propertiesManager!=null){
			return propertiesManager.getPropertySet().getString(name);
		}
		return "";
	}

	/**
	 * 
	 * @param text
	 */
	public String getText(String name){
		PropertiesManager propertiesManager = getPropertiesManager();
		if(propertiesManager!=null){
			return propertiesManager.getPropertySet().getText(name);
		}
		return null;
	}

	

	/**
	 * 
	 * @param name
	 * @param value
	 */
	public void setString(String name, String value){
		PropertiesManager propertiesManager = getPropertiesManager();
		if(propertiesManager != null){
			PropertySet propertySet = propertiesManager.getPropertySet();
			if(value == null){
				if(propertySet.exists(name)){
					propertySet.remove(name);
				}
			}else{
				propertySet.setString(name, value);
			}
		}
	}
	
	private Properties getDefaultProperties(){
		if(this.defaultProperties == null){
			this.defaultProperties = new Properties();
			InputStream in = ApplicationPropertiesImpl.class.getResourceAsStream(APP_PROPERTIES_DEFAULTS);
			try{
				this.defaultProperties.load(in);
				in.close();
			}catch(IOException e){
				log.error("不能从'{}'文件加载默认属性",APP_PROPERTIES_DEFAULTS);
			}
		}
		return this.defaultProperties;
	}

	/**
	 * 
	 * @param value
	 * @param key
	 */
	public void setText(String name, String value){
		PropertiesManager propertiesManager = getPropertiesManager();
		if(propertiesManager != null){
			PropertySet propertySet = propertiesManager.getPropertySet();
			if(value == null){
				if(propertySet.exists(name)){
					propertySet.remove(name);
				}
			}else{
				propertySet.setText(name, value);
			}
		}
	}
	

	public String getDefaultBackedText(String name) {
		String value = getText(name);
		if(value == null){
			value = getDefaultString(name);
		}
		return value;
	}

	@SuppressWarnings("unchecked")
	public Collection<String> getStringsWithPrefix(String preFix) {
		PropertiesManager propertiesManager = getPropertiesManager();
		if(propertiesManager != null){
			return propertiesManager.getPropertySet().getKeys(preFix,5);
		}
		return Collections.emptyList();
	}

}